package com.jf.cloud.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.jf.cloud.api.auth.constant.SysTypeEnum;
import com.jf.cloud.api.coupon.feign.CouponFeignClient;
import com.jf.cloud.api.discount.feign.DiscountFeignClient;
import com.jf.cloud.api.distribution.feign.DistributionFeignClient;
import com.jf.cloud.api.feign.SearchSpuFeignClient;
import com.jf.cloud.api.multishop.feign.ShopSubstituteSalesFeignClient;
import com.jf.cloud.common.cache.constant.ProductCacheNames;
import com.jf.cloud.common.cache.util.RedisUtil;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.constant.StatusEnum;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.i18n.I18nMessage;
import com.jf.cloud.common.product.bo.EsProductBO;
import com.jf.cloud.common.product.constant.*;
import com.jf.cloud.common.product.dto.SkuDTO;
import com.jf.cloud.common.product.vo.SkuLangVO;
import com.jf.cloud.common.product.vo.SkuVO;
import com.jf.cloud.common.product.vo.SpuSkuAttrValueVO;
import com.jf.cloud.common.product.vo.SpuVO;
import com.jf.cloud.common.product.vo.search.SpuAdminVO;
import com.jf.cloud.common.product.vo.search.SpuSearchVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.PriceUtil;
import com.jf.cloud.common.vo.ShopSubstituteSalesVO;
import com.jf.cloud.common.product.constant.SpuStatus;
import com.jf.cloud.product.mapper.*;
import com.jf.cloud.product.model.Sku;
import com.jf.cloud.product.model.SkuStock;
import com.jf.cloud.product.model.SpuConsignmentChange;
import com.jf.cloud.product.service.*;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zz
 * @date 2022/8/5 17:50
 */
@Service
public class SupplierSpuUpdateLaterOperateServiceImpl implements SupplierSpuUpdateLaterOperateService {
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private ShopSubstituteSalesFeignClient shopSubstituteSalesFeignClient;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private SpuOfflineService spuOfflineService;
    @Autowired
    private DistributionFeignClient distributionFeignClient;
    @Autowired
    private CouponFeignClient couponFeignClient;
    @Autowired
    private DiscountFeignClient discountFeignClient;
    @Autowired
    private SpuTagReferenceService spuTagReferenceService;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SkuService skuService;
    @Autowired
    private MapperFacade mapperFacade;
    @Autowired
    private SkuStockMapper skuStockMapper;
    @Autowired
    private SpuConsignmentChangeMapper spuConsignmentChangeMapper;
    @Autowired
    private SearchSpuFeignClient searchSpuFeignClient;
    @Autowired
    private SpuConsignmentChangeService spuConsignmentChangeService;


    @Override
    public void skuUpdateLaterOperate(Long supplierSpuId) {
        //获取所有代销商品信息
        List<SpuVO> commissionSpuLists = spuMapper.getCommissionSpuList(supplierSpuId);
        List<Long> commissionSpuIds = commissionSpuLists.stream().map(e -> e.getSpuId()).collect(Collectors.toList());
        //过滤掉自动设价的商品
        for (SpuVO commissionSpuList : commissionSpuLists) {
            //获取商家代销设置
            ShopSubstituteSalesVO substituteSalesVO = shopSubstituteSalesFeignClient.getByShopSubstituteSalesId(commissionSpuList.getShopId()).getData();
            if (Objects.equals(substituteSalesVO.getSalesType(), SalesType.SMART_PRICE.value())){
                List<Long> list = Arrays.asList(commissionSpuList.getSpuId());
                commissionSpuIds.removeAll(list);
            }
        }
        //下架手动设价的商品
        if (commissionSpuIds.size() != 0){
            batchChangeSpuStatus(commissionSpuIds, SpuStatus.OFF_SHELF.value(),SysTypeEnum.MULTISHOP.value());
        }
    }

    @Override
    public void skuWeightOrVolumeUpdateOperate(Long supplierSpuId) {
        supplierSpuUpdateLaterOffSpu(supplierSpuId, CommissionSpuOffType.SHOP_DELIVERY.value());
    }

    @Override
    public void skuAddOperate(Long supplierSpuId) {

    }

    @Override
    public void feePriceUnderSupplyPrice(EsProductBO esProductBO) {
        //获取商家代销设置
        ShopSubstituteSalesVO substituteSalesVO = shopSubstituteSalesFeignClient.getByShopSubstituteSalesId(esProductBO.getShopId()).getData();
        if (Objects.equals(substituteSalesVO.getSalesType(),SalesType.MANUAL_OPERATION_PRICE.value())){
            if (Objects.equals(substituteSalesVO.getAutomaticOff(),1)){
                //下架商品
                List<Long> spuIds = Arrays.asList(esProductBO.getSpuId());
                batchChangeSpuStatus(spuIds,SpuStatus.OFF_SHELF.value(),SysTypeEnum.MULTISHOP.value());
            }
        }
    }

    @Override
    public void batchChangeSpuStatus(List<Long> spuIds, Integer status,Integer sysType) {
        if (CollUtil.isEmpty(spuIds)) {
            return;
        }
        spuMapper.batchChangeSpuStatus(spuIds, status);
        // 更新品牌信息
        List<Long> brandIds = brandMapper.listBrandIdBySpuIds(spuIds);
        brandMapper.batchUpdateSpuCount(brandIds);
        if (Objects.equals(status, StatusEnum.DISABLE.value()) || Objects.equals(status, StatusEnum.OFFLINE.value())) {
            // 更新商品分组信息
            handleStatusChange(spuIds);
            // T删掉满减、优惠券的商品关联信息
            if (!Objects.isNull(AuthUserContext.get())){
                handleSpuStatusChange(spuIds);
            }else {
                handleCommissionSpuStatusChange(spuIds);
            }
            // 下线掉关联商品的活动 (在线下活动之前已经修改了商品的状态，下面的方法就查询不到已有的商品了，所以就需要带着修改后的商品去状态去查询)
            spuOfflineService.offlineCommissionSpuStatusAndActivity(1, spuIds, null, null, status,SysTypeEnum.MULTISHOP.value());
        } else if (Objects.equals(status, StatusEnum.ENABLE.value())) {
            handleDistributionSpuStatusChange(spuIds, status);
        }
    }

    @Override
    public void synMarketPrice(Long skuId, Long price) {
        List<Long> commissionSkuIds = skuMapper.getSkuIdsBySupplierSkuId(skuId);
        if (commissionSkuIds.size() != 0){
            List<Sku> skus = new ArrayList<>();
            for (Long commissionSkuId : commissionSkuIds) {
                Sku sku = new Sku();
                sku.setSkuId(commissionSkuId);
                sku.setMarketPriceFee(price);
                skus.add(sku);
            }
            if(CollUtil.isNotEmpty(skus)) {
                skuMapper.updateBatch(skus);
            }
        }
    }

    @Override
    public void soldOutManualPriceCommissionSpu(Long supplierSpuId,Integer type,Integer isFiltration) {
        //
        List<SpuVO> commissionSpuLists = spuMapper.getCommissionSpuList(supplierSpuId);
        // 手动设价商品id
        List<Long> manualSpuIds = new ArrayList<>();
        // 智能设价商品id
        List<Long> smartSpuIds = new ArrayList<>();
        for (SpuVO commissionSpuList : commissionSpuLists) {
            // 是否过滤商家自行发货的商品
            if (isFiltration != -1){
                if (Objects.equals(commissionSpuList.getSupplierDeliveryType(),SupplierDeliveryType.SHOP_DELIVERY.value())){
                    break;
                }
            }
            ShopSubstituteSalesVO data = shopSubstituteSalesFeignClient.getByShopSubstituteSalesId(commissionSpuList.getShopId()).getData();
            if (Objects.equals(data.getSalesType(),SalesType.MANUAL_OPERATION_PRICE.value())){
                manualSpuIds.add(commissionSpuList.getSpuId());
            }else {
                smartSpuIds.add(commissionSpuList.getSpuId());
            }
        }

        // 根据下架类型下架不同的代销商品
        if (Objects.equals(type,SalesType.SMART_PRICE.value())){
            // 下架智能设价
            if (smartSpuIds.size() != 0){
                // 下架商品
                batchChangeSpuStatus(smartSpuIds,SpuStatus.OFF_SHELF.value(),SysTypeEnum.MULTISHOP.value());
            }
        }

        if (Objects.equals(type,SalesType.MANUAL_OPERATION_PRICE.value())){
            // 下架手动设价
            if (manualSpuIds.size() != 0){
                // 下架商品
                batchChangeSpuStatus(manualSpuIds,SpuStatus.OFF_SHELF.value(),SysTypeEnum.MULTISHOP.value());
            }
        }


    }

    @Override
    public void supplierSkuAddOrDeleteLaterOperate(Long supplierSpuId,Long skuId) {
        SpuVO supplierSpu = spuMapper.getBySpuId(supplierSpuId);
        if (Objects.equals(supplierSpu.getSupplierSpuType(), SupplierSpuType.YES.value())) {
            SkuVO supplierSkuVO = skuMapper.getSkuBySkuId(skuId);
            //供应商sku信息
            List<SkuVO> skuVOS = skuMapper.listSkuWithAttrBySpuId(supplierSpuId);
            Map<Long, List<SpuSkuAttrValueVO>> supplierSkuAttrValueVO = skuVOS.stream().collect(Collectors.toMap(SkuVO::getSkuId, SkuVO::getSpuSkuAttrValues));
            SkuStock stockBySkuId = skuStockMapper.getBySkuId(skuId);
            supplierSkuVO.setSpuSkuAttrValues(supplierSkuAttrValueVO.get(skuId));
            if (supplierSkuVO.getSkuLangList().size() != 0){
                List<SkuLangVO> skuLangList = supplierSkuVO.getSkuLangList();
                Map<Integer, String> stringMap = skuLangList.stream().collect(Collectors.toMap(SkuLangVO::getLang, SkuLangVO::getSkuName));
                supplierSkuVO.setSkuName(stringMap.get(I18nMessage.getLang()));
            }
            supplierSkuVO.setStock(stockBySkuId.getStock());
            //供应商skuID对应的售价
            Map<Long, Long> supplierSkuIdPriceFee = skuVOS.stream().collect(Collectors.toMap(SkuVO::getSkuId, SkuVO::getPriceFee));
            //供应商skuID对应的市场价
            Map<Long, Long> supplierSkuIdMarketPriceFee = skuVOS.stream().collect(Collectors.toMap(SkuVO::getSkuId, SkuVO::getMarketPriceFee));
            //代销商品信息
            List<SpuVO> commissionSpuIdList = spuMapper.getCommissionSpuList(supplierSpuId);
            for (SpuVO spuVO : commissionSpuIdList) {
                //获取商家代销设置
                ShopSubstituteSalesVO salesVO = shopSubstituteSalesFeignClient.getByShopSubstituteSalesId(spuVO.getShopId()).getData();
                //获取代销商品sku信息
                List<SkuVO> commissionSkuVOs = this.listSkuWithAttrBySpuId(spuVO.getSpuId());
                List<String> commissionSkuNames = commissionSkuVOs.stream().map(e -> e.getSkuName()).collect(Collectors.toList());
                Map<String, Long> commissionSkuNameMap = commissionSkuVOs.stream().collect(Collectors.toMap(SkuVO::getSkuName, SkuVO::getSkuId));
                boolean contains = commissionSkuNames.contains(supplierSkuVO.getSkuName());
                //智能设价计算售价
                Long price = 0L;
                if (!Objects.isNull(salesVO)){
                    if (Objects.equals(salesVO.getSalesPriceType(), SalesPriceType.PROPORTION_PRICE.value())) {
                        //按比例加价
                        price = PriceUtil.divideByBankerRounding(supplierSkuVO.getPriceFee() * salesVO.getSalesPrice(), 10000) + supplierSkuVO.getPriceFee();
                    }
                    if (Objects.equals(salesVO.getSalesPriceType(), SalesPriceType.FIXED_NUMBER_PRICE.value())) {
                        //按固定数值加价
                        price = supplierSkuVO.getPriceFee() + salesVO.getSalesPrice();
                    }
                }
                if (contains) {
                    //存在,说明供应商新增的sku是之前删除的sku,同步改变代销商品中的供应商SkuId值和售价
                    Sku sku = new Sku();
                    sku.setSkuId(commissionSkuNameMap.get(supplierSkuVO.getSkuName()));
                    sku.setSupplierSkuId(supplierSkuVO.getSkuId());
                    //售价
                    if (!Objects.isNull(salesVO)){
                        if (salesVO.getSalesType() == 0){
                            sku.setPriceFee(price);
                        }
                        if (salesVO.getSalesType() == 1){
                            sku.setPriceFee(supplierSkuIdPriceFee.get(supplierSkuVO.getSkuId()));
                        }
                    }
                    //市场价
                    sku.setMarketPriceFee(supplierSkuIdMarketPriceFee.get(supplierSkuVO.getSkuId()));
                    skuMapper.update(sku);
                    return;
                }
                //不存在说明供应商sku属于新增的,同步新增
                if (salesVO.getSalesType() == 0) {
                    supplierSkuVO.setPriceFee(price);
                }
                supplierSkuVO.setMarketPriceFee(supplierSkuVO.getMarketPriceFee());
                SkuDTO skuDTO = mapperFacade.map(supplierSkuVO, SkuDTO.class);
                skuDTO.setSupplierSkuId(skuDTO.getSkuId());
                skuDTO.setSpuId(null);
                skuDTO.setStock(supplierSkuVO.getStock());
                List<SkuDTO> skuDTOS = Arrays.asList(skuDTO);
                if (skuDTOS.size() != 0){
                    skuService.commissionSave(spuVO.getSpuId(),skuDTOS,SysTypeEnum.MULTISHOP.value());
                }
            }
        }
    }

    @Override
    public void changeSynUpdate(Long supplierSpuId) {
        List<Long> commissionSpuIdList = spuMapper.getCommissionSpuIdList(supplierSpuId, null);
        Object delete = RedisUtil.get(ProductCacheNames.SKU_STATUS_DELETE + supplierSpuId);
        Object insert = RedisUtil.get(ProductCacheNames.SKU_STATUS_INSERT + supplierSpuId);
        //改变 供应商商品更新后代销商品是否已同步更新
        if (commissionSpuIdList.size() != 0){
            if (!Objects.isNull(delete)){
                spuMapper.batchChangeSynUpdate(commissionSpuIdList,IsSynUpdate.YEW_CHANGE.value());
            }
            for (Long aLong : commissionSpuIdList) {
                SpuVO spuVO = spuMapper.getBySpuId(aLong);
                //获取商家代销设置
                ShopSubstituteSalesVO salesVO = shopSubstituteSalesFeignClient.getByShopSubstituteSalesId(spuVO.getShopId()).getData();
                if (!Objects.isNull(salesVO)){
                    if (Objects.equals(salesVO.getSalesType(),0) && !Objects.isNull(insert)){
                        //智能设价时，供应商新增SKU时，编辑商品没有提示信息
                        spuMapper.batchChangeSynUpdate(commissionSpuIdList,IsSynUpdate.YEW_CHANGE.value());
                    }

                }
            }
            RedisUtil.del(ProductCacheNames.SKU_STATUS_INSERT + supplierSpuId);
        }
    }

    @Override
    public List<SpuAdminVO> disposeSpuMsg(List<SpuAdminVO> spuAdminList, Map<Long, List<SpuVO>> supplierSpuMap) {
        if (spuAdminList.size() == 0){
            return spuAdminList;
        }
        ShopSubstituteSalesVO salesVO = null;
        if (Objects.nonNull(spuAdminList.get(0).getShopId())) {
            salesVO = shopSubstituteSalesFeignClient.getByShopSubstituteSalesId(spuAdminList.get(0).getShopId()).getData();
        }
        List<Long> spuIdList = spuAdminList.stream().map(e -> e.getSpuId()).collect(Collectors.toList());
        //批量获取代销商品基本信息是否改变
        List<SpuConsignmentChange> spuConsignmentChanges = spuConsignmentChangeService.batchGetBySpuIdList(spuIdList);
        Map<Long, List<SpuConsignmentChange>> spuConsignmentChangeMap = new HashMap<>(16);
        if (spuConsignmentChanges.size() != 0 && Objects.nonNull(spuConsignmentChanges)){
            spuConsignmentChangeMap = spuConsignmentChanges.stream().collect(Collectors.groupingBy(SpuConsignmentChange::getSpuId));
        }
        for (SpuAdminVO spuAdminVO : spuAdminList) {
            if (Objects.isNull(spuAdminVO.getSupplierSpuId())){
                continue;
            }
            //返回代销设置类型
            if (Objects.nonNull(salesVO)){
                spuAdminVO.setSalesType(salesVO.getSalesType());
            }
            //判断代销商品是否选择了店铺分类
            if (Objects.nonNull(spuAdminVO.getShopCategoryId()) && spuAdminVO.getShopCategoryId() == -1){
                spuAdminVO.setIsSynUpdate(IsSynUpdate.NO_CHANGE.value());
            }
            //获取代销商品基本信息是否改变
            List<SpuConsignmentChange> consignmentChanges = spuConsignmentChangeMap.get(spuAdminVO.getSpuId());
            if (Objects.isNull(consignmentChanges)){
                continue;
            }
            SpuConsignmentChange spuConsignmentChange = consignmentChanges.get(0);
            //获取供应商信息
            SpuVO supplierSpu = supplierSpuMap.get(spuAdminVO.getSupplierSpuId()).get(0);
            List<Long> supplierSpuIds = Arrays.asList(spuAdminVO.getSupplierSpuId());
            List<SpuSearchVO> data = searchSpuFeignClient.listNotDeleteSpu(supplierSpuIds).getData();
            //没有数据说明供应商品已删除，
            if (data.size() == 0){
                continue;
            }

            if (Objects.equals(spuAdminVO.getSupplierDeliveryType(),SupplierDeliveryType.SUPPLIER_DELIVERY.value())){
                //名称
                if (Objects.equals(spuConsignmentChange.getNameChange(),0)){
                    spuAdminVO.setSpuName(supplierSpu.getName());
                }
                //图片
                if (Objects.equals(spuConsignmentChange.getImageChange(),0)){
                    spuAdminVO.setMainImgUrl(data.get(0).getMainImgUrl());
                }
                //spuCod
                if (Objects.equals(spuConsignmentChange.getSpuCodeChange(),0)){
                    spuAdminVO.setSpuCode(supplierSpu.getSpuCode());
                }
                //品牌
                if (Objects.equals(spuConsignmentChange.getBrandChange(),0)){
                    if (!Objects.isNull(supplierSpu.getBrand())){
                        spuAdminVO.setBrandName(supplierSpu.getBrand().getName());
                    }
                }
            }

        }
        return spuAdminList;
    }

    @Override
    public List<SpuSearchVO> usDisposeSpuMsg(List<SpuSearchVO> spuSearchVOList,Map<Long, List<SpuConsignmentChange>> spuConsignmentChangeMap,Map<Long, List<SpuVO>> supplierSpuMap) {
        for (SpuSearchVO spuSearchVO : spuSearchVOList) {
            if (Objects.isNull(spuSearchVO.getSupplierSpuId())){
                continue;
            }
            //获取代销商品基本信息是否改变
            if (Objects.isNull(spuConsignmentChangeMap.get(spuSearchVO.getSpuId()))){
                continue;
            }
            SpuConsignmentChange spuConsignmentChange = spuConsignmentChangeMap.get(spuSearchVO.getSpuId()).get(0);
            if (Objects.isNull(spuConsignmentChange)){
                continue;
            }
            //获取供应商信息
            SpuVO supplierSpu = supplierSpuMap.get(spuSearchVO.getSupplierSpuId()).get(0);
            List<Long> supplierSpuIds = Arrays.asList(spuSearchVO.getSupplierSpuId());
            List<SpuSearchVO> data = searchSpuFeignClient.listNotDeleteSpu(supplierSpuIds).getData();

            if (Objects.equals(spuSearchVO.getSupplierDeliveryType(),SupplierDeliveryType.SUPPLIER_DELIVERY.value())){
                //名称
                if (Objects.equals(spuConsignmentChange.getNameChange(),0)){
                    if (Objects.isNull(supplierSpu.getName())){
                        supplierSpu.setName("");
                    }
                    spuSearchVO.setSpuName(supplierSpu.getName());
                }
                //图片
                if (Objects.equals(spuConsignmentChange.getImageChange(),0)){
                    if (Objects.nonNull(data) && data.size() != 0 && Objects.nonNull(data.get(0).getMainImgUrl())){
                        spuSearchVO.setMainImgUrl(data.get(0).getMainImgUrl());
                    }
                }
                //spuCod
                if (Objects.equals(spuConsignmentChange.getSpuCodeChange(),0)){
                    spuSearchVO.setSpuCode(supplierSpu.getSpuCode());
                }
                //品牌
                if (Objects.equals(spuConsignmentChange.getBrandChange(),0)){
                    if (!Objects.isNull(supplierSpu.getBrand())){
                        spuSearchVO.setBrandName(supplierSpu.getBrand().getName());
                    }
                }
            }
        }
        return spuSearchVOList;
    }

    @Override
    public void supplierSpuUpdateLaterOffSpu(Long supplierSpuId, Integer commissionSpuOffType){
        if (Objects.equals(commissionSpuOffType,CommissionSpuOffType.ALL.value())){
            List<Long> commissionSpuIds = spuMapper.getSupIdListBySupplierId(supplierSpuId);
            //下架
            batchChangeSpuStatus(commissionSpuIds, SpuStatus.OFF_SHELF.value(),SysTypeEnum.MULTISHOP.value());
        }

        if (Objects.equals(commissionSpuOffType,CommissionSpuOffType.SUPPLIER_DELIVERY_SPU.value())){
            List<Long> commissionSpuIds = spuMapper.getCommissionSpuIdList(supplierSpuId,SupplierDeliveryType.SUPPLIER_DELIVERY.value());
            //下架
            batchChangeSpuStatus(commissionSpuIds, SpuStatus.OFF_SHELF.value(),SysTypeEnum.MULTISHOP.value());
        }

        if (Objects.equals(commissionSpuOffType,CommissionSpuOffType.SHOP_DELIVERY.value())){
            List<Long> commissionSpuIds = spuMapper.getCommissionSpuIdList(supplierSpuId,SupplierDeliveryType.SHOP_DELIVERY.value());
            //下架
            batchChangeSpuStatus(commissionSpuIds, SpuStatus.OFF_SHELF.value(),SysTypeEnum.MULTISHOP.value());
        }
    }

    private void handleStatusChange(List<Long> spuIds) {
        // 删除商品的分组信息
        spuTagReferenceService.deleteSpuData(spuIds);
    }

    private void handleSpuStatusChange(List<Long> spuIds) {
        List<Long> shopIds;
        if (Objects.equals(AuthUserContext.get().getTenantId(), Constant.PLATFORM_SHOP_ID)) {
            shopIds = new ArrayList<>();
        } else {
            shopIds = spuMapper.spuShopIdsBySpuIds(spuIds);
        }
        if (CollUtil.isNotEmpty(spuIds)) {
            ServerResponseEntity<Void> discountResponseEntity = discountFeignClient.handleSpuOffline(spuIds, shopIds);
            if (discountResponseEntity.isFail()) {
                throw new LuckException(discountResponseEntity.getMsg());
            }
            ServerResponseEntity<Void> couponResponseEntity = couponFeignClient.handleSpuOffline(spuIds, shopIds);
            if (couponResponseEntity.isFail()) {
                throw new LuckException(couponResponseEntity.getMsg());
            }
        }
    }

    private void handleCommissionSpuStatusChange(List<Long> spuIds) {
        List<Long> shopIds = spuMapper.spuShopIdsBySpuIds(spuIds);
        if (CollUtil.isNotEmpty(spuIds)) {
            ServerResponseEntity<Void> discountResponseEntity = discountFeignClient.handleSpuOffline(spuIds, shopIds);
            if (discountResponseEntity.isFail()) {
                throw new LuckException(discountResponseEntity.getMsg());
            }
            ServerResponseEntity<Void> couponResponseEntity = couponFeignClient.handleSpuOffline(spuIds, shopIds);
            if (couponResponseEntity.isFail()) {
                throw new LuckException(couponResponseEntity.getMsg());
            }
        }
    }

    private void handleDistributionSpuStatusChange(List<Long> spuIds, Integer status) {
        List<Long> shopIds;
        if (Objects.equals(AuthUserContext.get().getTenantId(), Constant.PLATFORM_SHOP_ID)) {
            shopIds = new ArrayList<>();
        } else {
            shopIds = spuMapper.spuShopIdsBySpuIds(spuIds);
        }
        ServerResponseEntity<Void> responseEntity = distributionFeignClient.handleSpuState(spuIds, shopIds, status);
        if(responseEntity.isFail()) {
            throw new LuckException(responseEntity.getMsg());
        }
    }

    public List<SkuVO> listSkuWithAttrBySpuId(Long spuId) {
        List<SkuVO> skuVOS = skuMapper.listSkuWithAttrBySpuId(spuId);
        for (SkuVO skuVO : skuVOS) {
            if (skuVO.getSkuLangList().size() != 0){
                skuVO.setSkuName(skuVO.getSkuLangList().get(0).getSkuName());
            }
            if (StrUtil.isNotBlank(skuVO.getImgUrl())) {
                skuVO.setMainImgUrl(skuVO.getImgUrl());
            }
        }
        return skuVOS;
    }
}
